// zh_gjw/pages/cash/cash.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
		cashcate: [],
		cashcateindex: 0,
		// message: '验证码',
		// currentTime: 60,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		var that = this
		getApp().util.request({
			'url': 'entry/wxapp/balance',
			'cachetime': '0',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				if (res.data.code == 200) {
					that.setData({
						allmoney: res.data.allmoney
					})
				}
			}
		});
		getApp().util.request({
			'url': 'entry/wxapp/Cashset',
			'cachetime': '0',
			success: function (res) {
				if (res.data.code == 200) {
					if(res.data.res.mode==2){
						that.setData({
							cashcate: ['微信']
						})
					}else if (res.data.res.mode == 1) {
						that.setData({
							cashcate: ['支付宝', '银行卡'],
							types:0
						})
					}else{
						that.setData({
							cashcate: ['微信', '支付宝', '银行卡'],
							types: 0
						})
					}
					that.setData({
						content: res.data.res.cashrule,
						min: res.data.res.min,
						mode: res.data.res.mode,
						rate: res.data.res.rate
					})
				}
			}
		});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
	bindPickerChange: function (e) {
		var that = this
		this.setData({
			cashcateindex: e.detail.value,
			types: e.detail.value,
		})
	}, 
	//获取电话号码
	onChange: function (e) {
		var phone = e.detail.value;
		this.setData({
			phone: phone
		})
	},
	//获取验证码
	onCode: function (e) {
		var that = this;
		var compphone = that.data.phone;
		var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;

		if (!myreg.test(compphone)) {
			wx.showToast({
				title: '您输入的手机号有误!',
				icon: "none",
				duration: 2000
			})
		} else {
			that.getCode();
			// // 获取6位数的随机数
			var recive_code = "";
			for (var i = 0; i < 6; i++) {
				recive_code += Math.floor(Math.random() * 10);
			}
			that.setData({
				disabled: true,
				recive_code: recive_code
			})
			// 随机数传给后台
			getApp().util.request({
				'url': 'entry/wxapp/Code',
				'cachetime': '0',
				data: {
					code: recive_code,
					phone: compphone
				},
				success: function (res) {

				}
			})
		}
	},
	//倒计时
	getCode: function (e) {
		var that = this;
		//输入的手机号
		var currentNum = that.data.phone;
		var currentTime = that.data.currentTime;
		var interval = setInterval(function () {
			currentTime--;
			that.setData({
				message: '剩' + currentTime + '秒'
			})
			if (currentTime <= 0) {
				clearInterval(interval)
				that.setData({
					message: "重新获取",
					currentTime: 60,
					disabled: false
				})
			}
		}, 1000)
	},
	formSubmit: function (e) {
		var that = this
		var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
		if (e.detail.value.phone == "") {
			wx.showToast({
				icon: "none",
				title: "请输入手机号码",
			})
		} else if (!(myreg.test(e.detail.value.phone))) {
			wx.showToast({
				icon: "none",
				title: "手机号码格式不正确",
			})
		} else if (that.data.recive_code != e.detail.value.code) {
			wx.showToast({
				icon: "none",
				title: "验证码错误",
			})
		}else{
			getApp().util.request({
				'url': 'entry/wxapp/Cashpassword',
				'cachetime': '0',
				data: {
					openid: wx.getStorageSync('openid'),
					password: e.detail.value.password
				},
				success: function (res) {
					if(res.data.code==200){
						getApp().util.request({
							'url': 'entry/wxapp/cashformsub',
							'cachetime': '0',
							data: {
								openid: wx.getStorageSync('openid'),
								formid: e.detail.formId,
								cash: e.detail.value.money,
								types: that.data.types,
								zfb: e.detail.value.zfb,
								yhk: e.detail.value.yhk,
								realname: e.detail.value.realname,
								phone: e.detail.value.phone,
								wechat: e.detail.value.wechat,
							},
							success: function (cres) {
								if(cres.data.code==200){
									wx.showToast({
										icon: "none",
										title: "提交成功",
									})
									wx.navigateBack({
										delta: 1,
									})
								} else if (cres.data.code == 500) {
									wx.showToast({
										icon: "none",
										title: "提交出错",
									})
								}
							}
						})
					} else if (res.data.code == 500){
						wx.showToast({
							icon: "none",
							title: "密码错误",
						})
					}
				}
			})
		}
	},
	formSubmits: function (e) {
		var that = this
		var realcash=0
		if(that.data.rate>0){
			realcash = (1 - that.data.rate * 0.01) * e.detail.value.money
		}else{
			realcash = e.detail.value.money
		}
		if (that.data.mode == 1 && that.data.types == 1){
			if (e.detail.value.yhname == "" || e.detail.value.yhname == undefined || e.detail.value.zhname == "" || e.detail.value.zhname == undefined || e.detail.value.name == "" || e.detail.value.name == undefined){
					wx.showToast({
						icon: "none",
						title: "请填写完整信息",
					})
			} else if (realcash > that.data.allmoney) {
				wx.showToast({
					icon: "none",
					title: "提现实际金额不能大于余额",
				})
			} else if (realcash < that.data.min) {
				wx.showToast({
					icon: "none",
					title: "提现实际金额不能小于" + that.data.min,
				})
			} else {
				getApp().util.request({
					'url': 'entry/wxapp/cashformsub',
					'cachetime': '0',
					data: {
						openid: wx.getStorageSync('openid'),
						formid: e.detail.formId,
						cash: e.detail.value.money,
						types: that.data.types,
						zfb: e.detail.value.zfb,
						yhk: e.detail.value.yhk,
						mode: that.data.mode,
						yhname: e.detail.value.yhname,
						zhname: e.detail.value.zhname,
						name: e.detail.value.name,
						realcash: realcash
						// realname: e.detail.value.realname,
						// phone: e.detail.value.phone,
						// wechat: e.detail.value.wechat,
					},
					success: function (cres) {
						if (cres.data.code == 200) {
							wx.showToast({
								icon: "none",
								title: "提交成功",
							})
							wx.navigateBack({
								delta: 1,
							})
						} else if (cres.data.code == 500) {
							wx.showToast({
								icon: "none",
								title: "提交出错",
							})
						}
					}
				})
			}
		}else if (that.data.mode == 0 && that.data.types == 2) {
			if (e.detail.value.yhname == "" || e.detail.value.yhname == undefined || e.detail.value.zhname == "" || e.detail.value.zhname == undefined || e.detail.value.name == "" || e.detail.value.name == undefined) {
				wx.showToast({
					icon: "none",
					title: "请填写完整信息",
				})
			} else if (realcash > that.data.allmoney) {
				wx.showToast({
					icon: "none",
					title: "提现实际金额不能大于余额",
				})
			} else if (realcash < that.data.min) {
				wx.showToast({
					icon: "none",
					title: "提现实际金额不能小于" + that.data.min,
				})
			} else {
				getApp().util.request({
					'url': 'entry/wxapp/cashformsub',
					'cachetime': '0',
					data: {
						openid: wx.getStorageSync('openid'),
						formid: e.detail.formId,
						cash: e.detail.value.money,
						types: that.data.types,
						zfb: e.detail.value.zfb,
						yhk: e.detail.value.yhk,
						mode: that.data.mode,
						yhname: e.detail.value.yhname,
						zhname: e.detail.value.zhname,
						name: e.detail.value.name,
						realcash: realcash
						// realname: e.detail.value.realname,
						// phone: e.detail.value.phone,
						// wechat: e.detail.value.wechat,
					},
					success: function (cres) {
						if (cres.data.code == 200) {
							wx.showToast({
								icon: "none",
								title: "提交成功",
							})
							wx.navigateBack({
								delta: 1,
							})
						} else if (cres.data.code == 500) {
							wx.showToast({
								icon: "none",
								title: "提交出错",
							})
						}
					}
				})
			}
		}else{
			 	if(realcash > that.data.allmoney) {
					wx.showToast({
						icon: "none",
						title: "提现实际金额不能大于余额",
					})
				} else if (realcash < that.data.min) {
					wx.showToast({
						icon: "none",
						title: "提现实际金额不能小于" + that.data.min,
					})
				} else {
					getApp().util.request({
						'url': 'entry/wxapp/cashformsub',
						'cachetime': '0',
						data: {
							openid: wx.getStorageSync('openid'),
							formid: e.detail.formId,
							cash: e.detail.value.money,
							types: that.data.types,
							zfb: e.detail.value.zfb,
							yhk: e.detail.value.yhk,
							mode: that.data.mode,
							yhname: e.detail.value.yhname,
							zhname: e.detail.value.zhname,
							name: e.detail.value.name,
							realcash: realcash
							// realname: e.detail.value.realname,
							// phone: e.detail.value.phone,
							// wechat: e.detail.value.wechat,
						},
						success: function (cres) {
							if (cres.data.code == 200) {
								wx.showToast({
									icon: "none",
									title: "提交成功",
								})
								wx.navigateBack({
									delta: 1,
								})
							} else if (cres.data.code == 500) {
								wx.showToast({
									icon: "none",
									title: "提交出错",
								})
							}
						}
					})
				}
		}
		
	},
})